package dao

import (
	"github.com/go-xorm/xorm"
	"imooc.com/demo_pms/models"
	"log"
)

type UserDao struct {
	engine *xorm.Engine
}

func NewUserDao(e *xorm.Engine) *UserDao {
	return &UserDao{engine: e}
}

// 参数正确与否，放在外面判断，能进这里默认参数都是正确的
func (u *UserDao) Create(data *models.UserInfo) error {
	_, err := u.engine.Insert(data)
	return err
}

func (u *UserDao) Update(data *models.UserInfo, cols []string) error {
	_, err := u.engine.Id(data.WorkId).
		MustCols(cols...).Update(data)
	return err
}

func (u *UserDao) Search(sql string) []models.UserInfo {
	userList := []models.UserInfo{}
	err := u.engine.SQL(sql).
		Where("sys_status=?", 1).
		GroupBy("part").Asc("").Find(&userList)
	if err != nil {
		log.Println("user_dao.Search error=", err)
	}
	return userList
}

func (u *UserDao) Query(sql string) ([]map[string][]byte, error) {
	rs := make([]map[string][]byte, 0)
	rs, err := u.engine.Query(sql)
	return rs, err
}

func (u *UserDao) BatchCreate(
	dataList []models.UserInfo) (int, error) {

	i, err := u.engine.Insert(dataList)
	return int(i), err
}
